# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def delNodes(self, root: TreeNode, to_delete: List[int]) -> List[TreeNode]:
        forests=[]
        def traverse(father,now,d):
            if now.val in to_delete:
                if father is not None:
                    if d=="l":
                        father.left=None
                    elif d=="r":
                        father.right=None
                if now.left:
                    traverse(None,now.left,'l')
                if  now.right:
                    traverse(None,now.right,'r')
            else:
                if father is None:
                    forests.append(now)
                if now.left:
                    traverse(now,now.left,'l')
                if now.right:
                    traverse(now,now.right,'r')
        traverse(None,root,'l')
        return forests

'''
执行用时：
76 ms
, 在所有 Python3 提交中击败了
74.78%
的用户
内存消耗：
15.3 MB
, 在所有 Python3 提交中击败了
59.29%
的用户
'''